/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.gis.repository;

import me.zhengjie.modules.gis.domain.TimeLine;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* @website https://el-admin.vip
* @author ZhuangBing Yan
* @date 2022-08-19
**/
public interface TimeLineRepository extends JpaRepository<TimeLine, Long>, JpaSpecificationExecutor<TimeLine> {
    @Query(value="  select * from time_line " +
            "where DATE_FORMAT(enter_in_time,'%Y-%m') = :enterInTime  ",
            countQuery = "  select count(1) from time_line " +
                    "where DATE_FORMAT(enter_in_time,'%Y-%m') = :enterInTime  ",
            nativeQuery = true
    )
    Page<TimeLine> findByEnterTimeLimit(@Param("enterInTime") String enterInTime, Pageable pageable);
    @Query(value="  select DISTINCT line_position from time_line " +
            "where DATE_FORMAT(enter_in_time,'%Y-%m') = :enterInTime  ",
            nativeQuery = true
    )
    List<String> findDistinctLine(String enterInTime);
    @Query(value="  select sum(gis_feed_history.net_weight) as weight, time_line.line_position as linePosition  from time_line left join gis_feed_history on time_line.id = gis_feed_history.id GROUP BY time_line.line_position ",
            nativeQuery = true
    )
    List<Map<String,Object>> findCurrentMonth(String enterInTime);
}